package com.yanqu.road.server.cmd.passRoad;

import com.yanqu.road.entity.player.UserPassRoad;
import com.yanqu.road.logic.pb.PassRoadPb;
import com.yanqu.road.pb.pass.RoadProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.passRoad.PassRoadModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_PASS_ROAD_BUY_BUFF, desc = "关卡买buff")
public class PassRoadBuyBuffCmd extends AbstractCommand {

	private int type;

	private int buyTimes;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		RoadProto.RoadBuyBuffReqMsg reqMsg = RoadProto.RoadBuyBuffReqMsg.parseFrom(packet.getBytes());
		type = reqMsg.getType();
		buyTimes = reqMsg.getBuyTimes();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ret = player.getModule(PassRoadModule.class).buyBuff(type, buyTimes);
	}

	@Override
	public void sendData(GamePlayer player) {
		RoadProto.RoadBuyBuffRespMsg.Builder respMsg = RoadProto.RoadBuyBuffRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			UserPassRoad userPassRoad = player.getModule(PassRoadModule.class).getUserPassRoad();
			RoadProto.RoadTempMsg.Builder passMsg = PassRoadPb.parsePassRoadTempMsg(userPassRoad);
			if(null != passMsg){
				respMsg.setPassData(passMsg);
			}
		}
		player.sendPacket(Protocol.U_PASS_ROAD_BUY_BUFF, respMsg);
	}
}
